import Vue from 'vue'
import App from './App.vue'
import CryptoJS from 'crypto-js'
import router from './router'
import store from './store'
import axios from 'axios'
import cookie from 'js-cookie'
import echarts from 'echarts'
import './plugins/iview.js'

Vue.config.productionTip = true

const KP = {
  key: 'scsxt7bd83asdf9asdf4vek9ue81jdlc', //秘钥 16*n:
  iv: 0  //偏移量
}

Vue.prototype.$http = axios;
// Vue.prototype.$API_URL = 'http://api.scs.cqapps.net';
Vue.prototype.$API_URL = 'http://api.scs.iclip365.com/';
Vue.prototype.$echarts = echarts;
// 超时时间
axios.defaults.timeout = 20000

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  store.state.showAppLoading = true
  return config
}, function (error) {
  console.log(error,'请求错误')
  store.state.showAppLoading = false
  // 对请求错误做些什么
  return Promise.reject(error)
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  store.state.showAppLoading = false
  var res = Vue.prototype.$twjsdes.decrypt(response.data.alldata);
  if (res.status != 1) {
    store.state.showErr = res.msg
  } else {

  }
  return response
}, function (error) {
  console.log(error,'请求错误')
  store.state.showAppLoading = false
  // 对响应错误做点什么
  return Promise.reject(error)
});

// 全局守卫
// router.beforeEach((to,from,next)=>{
//   console.log('来自：' + from.path)
//   console.log('前往：' + to.path)
//   next();
// })

Vue.prototype.$twjsdes = {
  enstr:function ( data ){
      //var uid=cookie.get('uid');
      //var token=cookie.get('token');
      var user = {
          uid: cookie.get('uid')?cookie.get('uid'):'',
          token: cookie.get('token')?cookie.get('token'):'',
          source: 'work'
      }
      var newdata = Object.assign(data, user);
      //console.log('post data:');
      //console.log(newdata);
      var strdata=this.encrypt(newdata)
      var postdata = {
          alldata: strdata,
      }

      return postdata;
  },
  encrypt:function ( data ){
    var data_json = JSON.stringify(data);
    var keyHex = CryptoJS.enc.Utf8.parse(KP.key);
    var encrypted = CryptoJS.DES.encrypt(data_json, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    var poststr = encrypted.toString();
    return poststr;    // 返回的是base64格式的密文
  },
  decrypt:function ( data ){
      var keyHex = CryptoJS.enc.Utf8.parse(KP.key);
      //var ivHex = CryptoJS.enc.Utf8.parse(KP.iv);
      var decrypted = CryptoJS.DES.decrypt({
          ciphertext: CryptoJS.enc.Base64.parse(data)
      }, keyHex, {
          mode: CryptoJS.mode.ECB,
          padding: CryptoJS.pad.Pkcs7
      });
      var decryptResult = JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));

      return decryptResult;
  },
  isWechat:function() {
	  let ua = window.navigator.userAgent.toLowerCase();
	  return ua.match(/MicroMessenger/i) == 'micromessenger';
  }
}


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
